/**
 * 初始化数据
 */

'use strict';

const mongoose = require('mongoose');
const User = mongoose.model('User');
const Article = mongoose.model('Article');
const TagCategory = mongoose.model('TagCategory');
const Tag = mongoose.model('Tag');
const Logs = mongoose.model('Logs');

//初始化标签,文章,用户
if (process.env.NODE_ENV === 'development') {
    User.countAsync().then(function (count) {
        if (count === 0) {
            User.removeAsync().then(function () {
                User.createAsync({
                    nickname: 'admin',
                    phone: '15764323489',
                    role: 'admin',
                    password: 'xiangdushen001',
                    slogan: '大渣好，我系渣渣辉',
                    status: 1
                }, {
                    nickname: '于涛',
                    phone: '1784312216',
                    role: 'user',
                    password: 'yut ao123',
                    slogan: '大渣好，我系于涛',
                    status: 1
                }, {
                    nickname: '蔡春锡',
                    phone: '18843100354',
                    role: 'user',
                    slogan: '大渣好，我系蔡春锡',
                    password: 'caichunxi123',
                    status: 2
                }, {
                    nickname: '李金宴',
                    phone: '18843104844',
                    role: 'user',
                    password: 'lijinyan123',
                    slogan: '大渣好，我系李金宴',
                    status: 0
                });
            });
        }
    });
    console.log('初始化');

    Logs.countAsync().then((count) => {
        if (count === 0) {
            Logs.removeAsync().then(() => {
                Logs.createAsync({
                    uid: '',
                    content: '项目上线运行',
                    type: 'system'
                });
            })
        }
    });


    TagCategory.countAsync().then(function (count) {
        if (count === 0) {
            TagCategory.removeAsync().then(function () {
                return Tag.removeAsync();
            }).then(function () {
                return TagCategory.createAsync({
                    name: 'draw',
                    desc: '绘画'
                }).then(function (cat) {
                    return Tag.createAsync({
                        name: '素描',
                        cid: cat._id,
                        is_show: true
                    }, {
                        name: '山水画',
                        cid: cat._id,
                        is_show: true
                    }, {
                        name: '动漫',
                        cid: cat._id,
                        is_show: true
                    })
                }).then(function () {
                    return TagCategory.createAsync({
                        name: 'handmade',
                        desc: '手工'
                    }).then(function (cat) {
                        return Tag.createAsync({
                            name: '剪纸',
                            cid: cat._id,
                            is_show: true
                        }, {
                            name: '模型',
                            cid: cat._id,
                            is_show: true
                        })
                    });
                }).then(function () {
                    return TagCategory.createAsync({
                        name: 'pic',
                        desc: '摄影'
                    }).then(function (cat) {
                        return Tag.createAsync({
                            name: '风景',
                            cid: cat._id,
                            is_show: true
                        }, {
                            name: '人物',
                            cid: cat._id,
                            is_show: true
                        })
                    });
                }).then(function () {
                    return TagCategory.createAsync({
                        name: 'cooking',
                        desc: '美食'
                    }).then(function (cat) {
                        return Tag.createAsync({
                            name: '中餐',
                            cid: cat._id,
                            is_show: true
                        }, {
                            name: '西餐',
                            cid: cat._id,
                            is_show: true
                        })
                    });
                }).then(function () {
                    return TagCategory.createAsync({
                        name: 'calligraphy',
                        desc: '书法'
                    }).then(function (cat) {
                        return Tag.createAsync({
                            name: '草书',
                            cid: cat._id,
                            is_show: true
                        }, {
                            name: '宋体',
                            cid: cat._id,
                            is_show: true
                        })
                    });
                }).then(function () {
                    return TagCategory.createAsync({
                        name: 'language',
                        desc: '外语'
                    }).then(function (cat) {
                        return Tag.createAsync({
                            name: '英语',
                            cid: cat._id,
                            is_show: true
                        }, {
                            name: '日语',
                            cid: cat._id,
                            is_show: true
                        })
                    });
                }).then(function () {
                    return TagCategory.createAsync({
                        name: 'process',
                        desc: '轻学习'
                    }).then(function (cat) {
                        return Tag.createAsync({
                            name: '轻学习',
                            cid: cat._id,
                            is_show: true
                        })
                    });
                }).then(function () {
                    return TagCategory.createAsync({
                        name: 'other',
                        desc: '其它分类'
                    }).then(function (cat) {
                        return Tag.createAsync({
                            name: '电脑技术',
                            cid: cat._id,
                            is_show: true
                        });
                    });
                }).then(function () {
                    return Tag.findAsync().then(function (tags) {
                        return tags;
                    })
                });

            }).then(function (tags) {
                return Article.removeAsync().then(function () {
                    return tags;
                });
            }).map(function (tag, index) {
                var indexOne = parseInt(index) + 1;
                var indexTwo = parseInt(index) + 2;
                Article.createAsync({
                    title: '第' + (index + indexOne) + '篇文章',
                    content: '<p>我第' + (index + indexOne) + '次爱你.</p>',
                    tags: [tag._id],
                    status: 1
                }, {
                    title: '第' + (index + indexTwo) + '篇文章',
                    content: '<p>我第' + (index + indexTwo) + '次爱你.</p>',
                    tags: [tag._id],
                    status: 1
                })
            });
        }
    })
}